package com.sgb.kjwl.view.ui.fragment.home;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.jicai.view.fragment.CentralizedMiningFragment5;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCentralizedMiningItemBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.home.CentralizedMining2Adapter;
import com.sgb.kjwl.viewmodel.home.CentralizedMiningItemModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.ClickUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 2020-6-04  产品王单：新版本 CentralizedMiningFragment3
 * 2020-7-15 新版本集采首页
 * Description:集采首页
 * Author zhao
 */
public class CentralizedMiningItemFragment extends AppLazyFragment {

    private static final String KEY_CENTRALIZED = "centralized";

    @Inject
    CentralizedMiningItemModel mModel;
    private FragmentCentralizedMiningItemBinding mBind;
    private CentralizedMining2Adapter mAdapter;

    private int mCurTabType;

    public static CentralizedMiningItemFragment newInstance(int position) {
        CentralizedMiningItemFragment fragment = new CentralizedMiningItemFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_CENTRALIZED, position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_centralized_mining_item, container, false);
        return mBind.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        if (getArguments() != null) {
            mCurTabType = (int) getArguments().getInt(KEY_CENTRALIZED);
        }
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this.getContext(), R.drawable.shape_list_divide_jc));
        mAdapter = new CentralizedMining2Adapter(getContext(), new ArrayList<>());
        mBind.recyclerView.addItemDecoration(divider);
        mBind.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBind.recyclerView.setAdapter(mAdapter);
//        mAdapter.setOnItemClick(new AdapterOnItemClick<CentralizedMiningEntity>() {
//            @Override
//            public void onItemClick(CentralizedMiningEntity centralizedMiningEntity, int position) {
//                mModel.getH5Url(centralizedMiningEntity.getNo());
//            }
//        });
        mAdapter.setItemClickCallBack(new CentralizedMining2Adapter.ItemClickCallBack() {
            @Override
            public void onItemClick(CentralizedMiningEntity entity) {
                if (!ClickUtils.isDoubleClick()) {
                    mModel.getH5Url(entity.getNo());
                }
            }
        });
    }


    protected void setRefresh() {
        mModel.getJiCaiList(mCurTabType, true);
    }

    @Override
    protected void onLazyLoad() {
//        mModel.getSecondAreaCodeList(mCurTabType);
        mModel.getJiCaiList(mCurTabType, false);
    }

    public void showList(List<CentralizedMiningEntity> list) {
        if (list != null && list.size() > 0) {
            mAdapter.setDatas(list);
            mBind.emptyView.setVisibility(View.GONE);
        } else {
            mAdapter.setDatas(null);
            mBind.emptyView.setVisibility(View.VISIBLE);
        }
        Fragment parentFragment = getParentFragment();
        if (parentFragment instanceof CentralizedMiningFragment5) {
            ((CentralizedMiningFragment5) parentFragment).hideRefresh();
        }
    }

    public void showUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }

    //    返回列表顶部
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(JiCaiEventEntity event) {
        if (event.getCode() == EventConstant.EVENT_JICAI_BACK_TO_TOP) {
            mBind.recyclerView.scrollToPosition(0);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
